package com.my.easypoi.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

public class ExcelUtils {

    /**
     * 判断合并了行
     *
     * @param sheet
     * @param row
     * @param column
     * @return
     */
    public static boolean isMergedRegion(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if (row >= firstRow && row <= lastRow) {
                if (column >= firstColumn && column <= lastColumn) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 获取合并单元格的值
     *
     * @param sheet
     * @param row
     * @param column
     * @return
     */
    public static String getMergedRegionValue(Sheet sheet, int row, int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();

        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress ca = sheet.getMergedRegion(i);
            int firstColumn = ca.getFirstColumn();
            int lastColumn = ca.getLastColumn();
            int firstRow = ca.getFirstRow();
            int lastRow = ca.getLastRow();

            if (row >= firstRow && row <= lastRow) {

                if (column >= firstColumn && column <= lastColumn) {
                    Row fRow = sheet.getRow(firstRow);
                    Cell fCell = fRow.getCell(firstColumn);
                    return getCellValue(fCell);
                }
            }
        }

        return null;
    }

    /**
     * 获取单元格的值
     *
     * @param cell
     * @return
     */
    public static String getCellValue(Cell cell) {
        if (cell == null) return "";
        if (cell.getCellType().getCode()== CellType.STRING.getCode()) {
            return cell.getStringCellValue();
        } else if (cell.getCellType().getCode() == CellType.BOOLEAN.getCode()) {
            return String.valueOf(cell.getBooleanCellValue());
        } else if (cell.getCellType().getCode() == CellType.FORMULA.getCode()) {
            return cell.getCellFormula();
        } else if (cell.getCellType().getCode() == CellType.NUMERIC.getCode()) {
            return String.valueOf(cell.getNumericCellValue());
//            double numericCellValue = cell.getNumericCellValue();
//            if(numericCellValue>100000){
////                if (numericCellValue == (int) numericCellValue) {
//                return String.valueOf(BigInteger.valueOf((long) numericCellValue));
////                } else {
////                    return String.valueOf(numericCellValue);
////                }
////                return cell.getStringCellValue();
//            }
        }
        return "";
    }

}
